package com.nageoffer.shortlink.admin.config;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.nageoffer.shortlink.admin.common.util.DateUtil;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * 自定义 p6spy sql输出格式
 */
@Configuration
public class P6spySqlFormatConfigure implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? DateUtil.dateToString(LocalDateTime.now(), DateUtil.DATEFORMATMINUTE) + " | " +MDC.get("trackId")
                + " | 耗时 " + elapsed + " ms | SQL 语句：" + "\n" + sql.replaceAll("[\\s]+"," ") + ";" : "";
    }
}
